﻿using LivestreamE_Commerce.Dto.CreateUpdateDto;
using LivestreamE_Commerce.Dto.GoodsDto;
using LivestreamE_Commerce.Dto.RBACDto;
using LivestreamE_Commerce.IService.GoodsIService;
using LivestreamE_Commerce.Model.Goods;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace LivestreamE_Commerce.Service.GoodsService
{
    public class MyGoodsService : CrudAppService<GoodsModel, GoodsDto, Guid, PagedAndSortedResultRequestDto, CreateUpdateGoodsDto>, IMyGoodsService
    {
        CSRedis.CSRedisClient client = new CSRedis.CSRedisClient("127.0.0.1:6379");
        public MyGoodsService(IRepository<GoodsModel, Guid> repository) : base(repository)
        {

        }
        [HttpPost, Route("caradd")]
        public List<CarViewModel> CarAdd(CarViewModel model)
        {
            List<CarViewModel> list = new List<CarViewModel>();
            var key = $"user{model.UserId}";
            var mass = client.Get(key);
            if (mass == null)
            {
                list.Add(model);
                client.Set(key, list);
            }
            else
            {
                var _list = JsonConvert.DeserializeObject<List<CarViewModel>>(mass);
                var good = _list.Where(x => x.GoodId == model.GoodId).FirstOrDefault();
                if (good == null)
                {
                    _list.Add(model);
                }
                else
                {
                    good.Number += model.Number;
                }
                client.Set(key, _list);
            }
            return list;
        }
    }
    public class CarViewModel
    {
        public int UserId { get; set; }
        public int GoodId { get; set; }
        public int Number { get; set; }

    }
}
